/**
 * Created by dukai on 2017/5/11.
 */
MTMgtController = function () {
   var user = sessionStorageClient.getJSON("user");
    // 机具列表
    this.getMTRecords = function (reset) {
      
        //toast 请稍后...
       common.loading("请稍后...");

        var serverUrl = App.serverUrl + "app/machineTool/getMachineToolList.do";
        // var serverUrl = "data4MT.json";

        var mtParams = {};
        mtParams.pageSize = mtSearchParam.pageSize;
        mtParams.currentPage = mtSearchParam.currentPage;

        mtParams.shopId =user.role==4?user.selfShopId:mtSearchParam.shopId;

        if(mtParams.shopName=="全部门店"){
            mtParams.shopId = "";
        }

        mtParams.machineToolType = mtSearchParam.machineToolType;
        mtParams.orderSource = mtSearchParam.orderSource;


        var sucFunc = function (result) {

            log4JS.info("机具管理列表信息："+ JSON.stringify(result));

            if (result.success) {

                var pageData = result.resultObject;
                  $("#mtMgtPage .page-body .mtListContainer .no-data-wrap").hide();
                //列表为空
                if(pageData.currPage==1 && pageData.list==0){
                    //CommonUtil.render($("#mtMgtPage .page-body .mtListContainer .no-wrap"), "mtTPLEmpty", {mts: pageData.list});
                    $("#mtMgtPage .page-body .mtListContainer .no-data-wrap").show();
                    //调reset方法使下拉框回去
                   reset && reset();
                    return;
                }

                if (pageData.nextPage > 0) {
                  
                    // 显示更多
                    // initAddMoreItem($("#mtMgtPage .page-body"));
                    reset && reset();
                } else {
                    // 没有更多
                    reset && reset(true);
                    disableAddMoreItem($("#mtMgtPage .page-body"));
                }
                CommonUtil.render($("#mtMgtPage .page-body .mtListContainer .list"), "mtTPL", {mts: pageData.list},mtSearchParam.currentPage > 1);
            } else {
                DialogUtil.dialogWarn(result.msg);
                reset && reset();
            }
        };

        var failFunc = function () {
            log4JS.info("getMTRecords failed. http status not 200");
            DialogUtil.dialogWarn();
            reset && reset();
        };


        // CommonUtil.ajaxRequest(serverUrl, "POST", JSON.stringify(XXX), sucFunc, failFunc);
        CommonUtil.sendJsonpRequest({"url":serverUrl,"params":mtParams,"success":sucFunc,"error":failFunc});

    };


    // 门店列表
    this.getShopList = function () {

        //toast 请稍后...
        common.loading("请稍后...");

        var serverUrl = App.serverUrl + "app/machineTool/getShopSelect.do";

        var sucFunc = function (result) {

            log4JS.info("机具管理-门店列表信息："+ JSON.stringify(result));

            if (result.success ) {
                var data = result.resultObject;
                // var list = data.list;
                // CommonUtil.render($("#mtMgtPage .page-body"), "mtTPL", {mts: data.list});
                CommonUtil.render($("#mtMgtShopListPage .page-body"), "mtMgtShopListTpl", {"cityList":data});

                if(mtSearchParam.shopName){

                    var target = null;

                    if(!mtSearchParam.useCachePage2){
                        $("#mtMgtShopListPage li").removeClass("selected");
                        $("#mtMgtShopListPage .check-all").addClass("selected");
                    }else if(mtSearchParam.shopName=="全部门店"){
                        $("#mtMgtShopListPage li").removeClass("selected");
                        $("#mtMgtShopListPage .check-all").addClass("selected");
                    }
                    else{
                        $("#mtMgtShopListPage .check-all").removeClass("selected");
                        $("#mtMgtShopListPage .store-list li").each(function(){
                            if($(this).find("p").html()==mtSearchParam.shopName){
                                $(this).addClass("selected");
                            }
                        })
                    }
                }

            } else {
                DialogUtil.dialogWarn(result.msg);
                // reset && reset();
            }
        };

        var failFunc = function () {
            log4JS.info("getShopList failed. http status not 200");
            DialogUtil.dialogWarn();
            // reset && reset();
        };


        // CommonUtil.ajaxRequest(serverUrl, "POST", JSON.stringify(XXX), sucFunc, failFunc);
        CommonUtil.sendJsonpRequest({"url":serverUrl,"params":null,"success":sucFunc,"error":failFunc});

    };
}